<?php

  class Reservering
  {
    public $les_ID;
    public $user_ID;

    function __construct($les_id, $user_id)
    {
      $this->les_ID  = $les_id;
      $this->user_ID = $user_id;
    }    
    
    public static function by_week($week, $year)
    {
      $query  = mysql_query('SELECT DISTINCT '.DB_LES.'.dag, '.DB_LES.'.tijd, leden.voornaam, leden.achternaam, leden.email FROM '.DB_LES.' INNER JOIN '.DB_RESERVERING.' INNER JOIN leden ON ('.DB_RESERVERING.'.les_ID = '.DB_LES.'.ID AND '.DB_RESERVERING.'.user_ID = leden.ID) WHERE week="'.$week.'" AND jaar="'.$year.'" ORDER BY '.DB_LES.'.dag');
      
      while ($row = mysql_fetch_assoc($query))
        $reserveringen[] = $row;

      return $reserveringen;
    }

    public static function count_reservering_per_week($week, $jaar)
    {
      $query  = mysql_query('SELECT COUNT('.DB_LES.'.ID) FROM '.DB_LES.' INNER JOIN '.DB_RESERVERING.' INNER JOIN leden ON ('.DB_RESERVERING.'.les_ID = '.DB_LES.'.ID AND '.DB_RESERVERING.'.user_ID = leden.ID) WHERE week="'.$week.'" AND jaar="'.$jaar.'"');
      $row    = mysql_fetch_assoc($query);
      $aantal = $row['COUNT('.DB_LES.'.ID)'];

      $message = "reserveringen  " . Website::create_link("reserveringen","tonen");

      switch ($aantal)
      {
        case 0  : return "Er zijn deze week <span class='nr'>(geen)</span> reserveringen.";
        case 1  : return "Er is deze week <span class='nr'>(1)</span> $message";
        default : return "Er zijn deze week <span class='nr'>(".$aantal.")</span> $message";
      }
    }

    public static function count_reservering_per_dag($dag, $week, $jaar, $tijd)
    {
      $query  = mysql_query('SELECT COUNT('.DB_LES.'.ID) FROM '.DB_LES.' INNER JOIN '.DB_RESERVERING.' INNER JOIN leden ON ('.DB_RESERVERING.'.les_ID = '.DB_LES.'.ID AND '.DB_RESERVERING.'.user_ID = leden.ID) WHERE dag="'.$dag.'" AND week="'.$week.'" AND jaar="'.$jaar.'" AND tijd="'.$tijd.'"');
      $row    = mysql_fetch_assoc($query);
      return $row['COUNT('.DB_LES.'.ID)'];
    }

    public static function send_note($les_id, $user_id)
    {
      $user = User::by_id($user_id);
      $les  = Les::by_id($les_id);

      $to      = $user->email;
      $subject = '[4anybody] U kunt spinnen op de '.$les->day.'e ('.$les->weekday.')';
      $message = "Hoi ". $user->firstname . " " . $user->lastname . ",\nU stond op de reservelijst, nu heeft iemand zich afgemeld en kunt u dus Spinnen!\n\nDag ".$les->day."(".$les->weekday.") in week ".$les->week." om ".$les->time."\nHopelijk kunt u nog steeds anders moet u zich even afmelden.\n\nVriendelijke Groeten, \nNancy";
      $headers = 'From: spinning-steps@4anybody.nl' . PHP_EOL .
          'Reply-To: spinning-steps@4anybody.nl' . PHP_EOL .
          'X-Mailer: PHP/' . phpversion();

      mail($to, $subject, $message, $headers);
    }
  }
?>